package org.ncibi.cytoscape.metscape.network;

import org.ncibi.cytoscape.metscape.data.Attributes;
import org.ncibi.cytoscape.metscape.data.GeneData;
import org.ncibi.cytoscape.metscape.data.Organism;
import org.ncibi.cytoscape.metscape.utils.NumUtils;
import org.ncibi.metab.network.attribute.GeneAttribute;
import org.ncibi.metab.network.node.GeneNode;
import org.ncibi.metab.network.node.MetabolicNode;

public class GeneNodeTranslator extends AbstractNodeTranslator {

    
	private GeneData geneData;
	private Organism organism;
	
	public GeneNodeTranslator(GeneData geneData, Organism organism) {
		this.geneData = geneData;
		this.organism = organism;
	}
	
	protected String getCanonicalName(MetabolicNode node) {
    	return GeneNode.getSymbol(node);
    }

	protected void addAttributes(String id, MetabolicNode node){
		Integer orgId = GeneNode.getOrganismGeneid(node);
		
		if(GeneNode.getHomologGeneid(node) != null) {
			Attributes.node.setAttribute(id, "Gene." + organism.toString().toLowerCase() 
					+ ".geneid", GeneNode.getHomologGeneid(node));
		}
		Attributes.node.setAttribute(id, GeneAttribute.GENEID.toAttributeName(), GeneNode.getGeneid(node));
		Attributes.node.setAttribute(id, GeneAttribute.SYMBOL.toAttributeName(), GeneNode.getSymbol(node));
		Attributes.node.setAttribute(id, GeneAttribute.DESCRIPTION.toAttributeName(), GeneNode.getDescription(node));
		Attributes.node.setListAttribute(id, GeneAttribute.LOCATIONS.toAttributeName(), GeneNode.getLocations(node));
		String name = geneData.getName();
		if(geneData.getData(orgId) != null) {
			Double[] data = geneData.getData(orgId);
			String[] columns = geneData.getColumns();
			Attributes.node.setAttribute(id, "Gene." + name + ".hasData", true);
			for(int i=0; i<data.length; i++) {
				Attributes.node.setAttribute
				(id,"Gene." + name + "." + columns[i], NumUtils.truncate(data[i]));
			}
		}
		else if(!geneData.isEmpty()) {
			Attributes.node.setAttribute(id, "Gene." + name + ".hasData", false);
		}
	}
}
